﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using System.Collections.Generic;

public class AudioManager : GameComponent
{
    private static AudioManager audioManager = null;

    SoundEffectInstance playingMusic;
    string playingMusicName = "";
    ContentManager content;

    public static float SoundVolume = 0.4f;

    private static float musicVolume = 0.7f;
    public static float MusicVolume
    {
        get { return musicVolume; }
        set
        {
            musicVolume = value;
            audioManager.playingMusic.Volume = musicVolume;
        }
    }

    private AudioManager(Game game) : base(game)
    {
        content = game.Content;
    }

    public static void Initialize(Game game)
    {
        audioManager = new AudioManager(game);
        if (game != null)
        {
            game.Components.Add(audioManager);
        }
    }

    public static void PlaySound(string sound)
    {
        SoundEffect soundEffect = audioManager.content.Load<SoundEffect>(sound);
        if(!Globals.Mute)
            soundEffect.Play(SoundVolume,0,0);
    }

    public static void PlayMusic(string music)
    {
        if (!audioManager.playingMusicName.Equals(music))
        {
            if(audioManager.playingMusic != null)
                audioManager.playingMusic.Stop();
            SoundEffect soundEffect = audioManager.content.Load<SoundEffect>(music);
            audioManager.playingMusicName = music;
            audioManager.playingMusic = soundEffect.CreateInstance();
            audioManager.playingMusic.IsLooped = true;
            audioManager.playingMusic.Volume = MusicVolume;
            if (!Globals.Mute)
                audioManager.playingMusic.Play();
        }
    }

    public static void StopMusic()
    {
        audioManager.playingMusic.Stop();
    }

    public static void ContinueMusic()
    {
        audioManager.playingMusic.Play();
    }

    public override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
    }

}